<meta charset="utf-8">
(#) Calling new methods on older versions

!!! ERROR: Calling new methods on older versions
   This is an error.

Id
:   `NewApi`
Summary
:   Calling new methods on older versions
Severity
:   Error
Category
:   Correctness
Platform
:   Android
Vendor
:   Android Open Source Project
Feedback
:   https://issuetracker.google.com/issues/new?component=192708
Since
:   Initial
Affects
:   Kotlin and Java files, manifest files and resource files
Editing
:   This check runs on the fly in the IDE editor
Implementation
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/ApiDetector.kt)
Tests
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/ApiDetectorTest.java)
Copyright Year
:   2012

This check scans through all the Android API calls in the application
and warns about any calls that are not available on **all** versions
targeted by this application (according to its minimum SDK attribute in
the manifest).

If you really want to use this API and don't need to support older
devices just set the `minSdkVersion` in your `build.gradle` or
`AndroidManifest.xml` files.

If your code is **deliberately** accessing newer APIs, and you have
ensured (e.g. with conditional execution) that this code will only ever
be called on a supported platform, then you can annotate your class or
method with the `@TargetApi` annotation specifying the local minimum SDK
to apply, such as `@TargetApi(11)`, such that this check considers 11
rather than your manifest file's minimum SDK as the required API level.

If you are deliberately setting `android:` attributes in style
definitions, make sure you place this in a `values-v`*NN* folder in
order to avoid running into runtime conflicts on certain devices where
manufacturers have added custom attributes whose ids conflict with the
new ones on later platforms.

Similarly, you can use tools:targetApi="11" in an XML file to indicate
that the element will only be inflated in an adequate context.

!!! Tip
   This lint check has an associated quickfix available in the IDE.

(##) Example

Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/test.kt:6:Error: Call requires API level 23 (current min is 21):
android.net.ConnectivityManager#getActiveNetwork [NewApi]
    val network = cm.activeNetwork // Error: Requires API 23
                     -------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here are the relevant source files:

`src/AndroidManifest.xml`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~xml linenumbers
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    &lt;uses-sdk
        android:minSdkVersion="21"
        android:targetSdkVersion="30" /&gt;
&lt;/manifest&gt;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`src/test.kt`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
import android.content.Context
import android.net.ConnectivityManager
import android.os.Build
fun test(context: Context) {
    val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val network = cm.activeNetwork // Error: Requires API 23
    if (Build.VERSION.SDK_INT >= 23) {
        val network2 = cm.activeNetwork // OK
    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also visit the
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/ApiDetectorTest.java)
for the unit tests for this check to see additional scenarios.

(##) Suppressing

You can suppress false positives using one of the following mechanisms:

* Adding the suppression attribute `tools:ignore="NewApi"` on the
  problematic XML element (or one of its enclosing elements). You may
  also need to add the following namespace declaration on the root
  element in the XML file if it's not already there:
  `xmlns:tools="http://schemas.android.com/tools"`.

* Using a suppression annotation like this on the enclosing
  element:

  ```kt
  // Kotlin
  @Suppress("NewApi")
  fun method() {
     problematicStatement()
  }
  ```

  or

  ```java
  // Java
  @SuppressWarnings("NewApi")
  void method() {
     problematicStatement();
  }
  ```

* Using a suppression comment like this on the line above:

  ```kt
  //noinspection NewApi
  problematicStatement()
  ```

* Using a special `lint.xml` file in the source tree which turns off
  the check in that folder and any sub folder. A simple file might look
  like this:
  ```xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;lint&gt;
      &lt;issue id="NewApi" severity="ignore" /&gt;
  &lt;/lint&gt;
  ```
  Instead of `ignore` you can also change the severity here, for
  example from `error` to `warning`. You can find additional
  documentation on how to filter issues by path, regular expression and
  so on
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).

* In Gradle projects, using the DSL syntax to configure lint. For
  example, you can use something like
  ```gradle
  lintOptions {
      disable 'NewApi'
  }
  ```
  In Android projects this should be nested inside an `android { }`
  block.

* For manual invocations of `lint`, using the `--ignore` flag:
  ```
  $ lint --ignore NewApi ...`
  ```

* Last, but not least, using baselines, as discussed
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).

<!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://morgan3d.github.io/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>